package main.com.controller;

import main.com.Help.isNumber;
import main.com.pojo.Text;
import main.com.service.ElasticService;
import main.com.service.TextService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class TextController {

    @Autowired
    private TextService textService;

    @Autowired
    private ElasticService elasticService;

    @RequestMapping("/add")
    public String add(){
        return "add.html";
    }

    @RequestMapping("/formalAdd")
    @ResponseBody
    public String formalAdd(Text text) throws Exception{
        if(text.getSubTitle()==null||text.getPrice()==null||text.getTitle()==null){
            return "增加失败，title,subtitle,price都不为空";
        }else{
            int i = textService.insert(text);
            if(i!=0){

                text.setId(i);
                boolean b = elasticService.saveOrUpdate(text);
                return "增加成功";
            }else{
                return "增加失败";
            }
        }
    }

    @RequestMapping("/delete")
    public String delete(){
        return "delete.html";
    }

    @RequestMapping("/formalDelete")
    @ResponseBody
    public String formalAdd(String id) throws Exception{
        if(id==null||id==""||!isNumber.isNumber(id)){
            return "请输入有效id";
        }else{
            int m = Integer.parseInt(id);
            int i = textService.delete(m);
            if(i!=0){
                boolean b = elasticService.deleteById(m);
                return "删除成功";
            }else{
                return "删除失败,数据库没有您输入的Id";
            }
        }
    }

    @RequestMapping("/update")
    public String update(){
        return "update.html";
    }

    @RequestMapping("/formalDelete1")
    public String formalDelete1(String id, HttpServletRequest request){
        if(id==null||id==""||!isNumber.isNumber(id)){
            return "error1.html";
        }else{
            int i = Integer.parseInt(id);
            Text text = textService.select(i);
            request.setAttribute("text",text);
            return "formalDelete.html";
        }
    }

    @RequestMapping("/formalDelete2")
    @ResponseBody
    public String formalDelete2(int id,String title,String subtitle,String price) throws Exception{
        if(price==null||price==""||!isNumber.isDouble(price)){
            return "price输入不合理";
        }else{
            Text text = new Text();
            text.setId(id);
            text.setTitle(title);
            text.setSubTitle(subtitle);
            text.setPrice(Double.parseDouble(price));

            int i = textService.update(text);
            if(i==0){
                return "修改失败";
            }else{
                boolean b = elasticService.saveOrUpdate(text);
                return "修改成功";
            }
        }
    }

    @RequestMapping("/select")
    public String select(HttpServletRequest request){
        List<Text> texts = textService.select();
        request.setAttribute("texts",texts);
        return "select";
    }

    @RequestMapping("/es_select")
    public String esselect(HttpServletRequest request,String titleOrSubTitle) throws Exception{
        List<Text> texts = textService.select();
        request.setAttribute("texts",texts);
        if(titleOrSubTitle.isEmpty()){
            return "error2";
        }else{
            List<Text> texts1 = elasticService.queryMultiMatch(titleOrSubTitle);
            for (Text t : texts1) {
                System.out.println(t.toString());
            }
            request.setAttribute("b",texts1);
            return "select";
        }
    }

}

